package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annation.*;
import com.example.http.HttpRespServer;
import com.example.model.FormMulit;
import com.example.socket.ServletRequest;
import com.example.util.FileUtils;
import com.example.util.Results;

import java.io.File;
import java.io.IOException;

/**
 * @Classname TestController
 * @Description TODO
 * @Date 2023/4/24 15:39
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/test")
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("/test")
    public JSONObject testApi(@Param("name")String name){
        JSONObject object=testService.getJSONObject("hahah1"+name);
        return object;
    }

    @GetMapping("test2")
    public JSONObject testApi2(ServletRequest request, @Param("name")String name){
        System.out.println(name);
        JSONObject obj=new JSONObject();
        obj.put("code",2);
        obj.put("msg","你访问到我了111");
        return obj;
    }



    @PostMapping("/upload")
    public JSONObject uploadFile(FormMulit formMulit) throws IOException {
        if(formMulit!=null){
            System.out.println(formMulit.getFileName()+"文件名");
            System.out.println(formMulit.getFileType()+"文件类型");
            formMulit.wirteFile(formMulit.getBytes(),"E:\\文件下载\\"+formMulit.getFileName());
        }
        return testService.getJSONObject("上传成功222222");
    }

    @GetMapping("/donload")
    public JSONObject donload(HttpRespServer httpRespServer) throws IOException {
        File file=new File("D:\\Users\\X.I.O\\Pictures\\Camera Roll\\IMG_2073.MOV");
        byte[] bytes=FileUtils.getBytesByPath("D:\\Users\\X.I.O\\Pictures\\Camera Roll\\IMG_2073.MOV");
        httpRespServer.writeByte(bytes,file.getName());
        return Results.success("下载");

    }
}
